package com.xiaoerkeji.eshop.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.util.TypeUtils;

import java.util.List;

import androidx.annotation.Nullable;

/**
 * Created by tianlan on 2018/1/25.
 */

public class JsonHelper
{

    @Nullable
    public static <T> List<T> parseArray(String text, Class<T> clazz) {
        try {
            return JSON.parseArray(text, clazz);
        } catch (com.alibaba.fastjson.JSONException e) {
            e.printStackTrace();
            return null;
        }
    }


    @Nullable
    public static <T> T parseObject(String text, Class<T> clazz) {
        try {
            return JSON.parseObject(text, clazz);
        } catch (com.alibaba.fastjson.JSONException e) {
            e.printStackTrace();
            return null;
        }
    }

//    @Nullable
//    public static <T> T parseObject(JSONObject jsonObject, Class<T> clazz) {
//        try {
//            return JSON.toJavaObject(jsonObject, clazz);
//        } catch (com.alibaba.fastjson.JSONException e) {
//            e.printStackTrace();
//            return null;
//        }
//    }

    public static JSONObject toJsonObject(Object o) {
        try {
            return JSON.parseObject(toJson(o));
        } catch (JSONException | NullPointerException | ClassCastException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String toJson(Object o) {
        return JSON.toJSONString(o);
    }

    public static Object get(String jsonStr, String key) {
        try {
            JSONObject jsonObject = JSON.parseObject(jsonStr);
            if (jsonObject.containsKey(key)) {
                return jsonObject.get(key);
            }
        } catch (JSONException | ClassCastException | NullPointerException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Object get(JSONObject jsonObject, String key) {
        try {
            if (jsonObject.containsKey(key)) {
                return jsonObject.get(key);
            }
        } catch (JSONException | ClassCastException | NullPointerException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void put(JSONObject jsonObject, String key, Object value) {
        if (jsonObject == null || key == null || value == null) {
            return;
        }
        try {
            jsonObject.put(key, value);
        } catch (com.alibaba.fastjson.JSONException e) {
            e.printStackTrace();
        }
    }

    public static boolean getBool(String json, String key) {
        try {
            return Boolean.TRUE.equals(TypeUtils.castToBoolean(get(json, key)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static String[] getStringArr(JSONObject jsonObject, String key) {
        try {
            JSONArray jsonArray = jsonObject.getJSONArray(key);
            if (jsonArray != null) {
                String[] strings = new String[jsonArray.size()];
                for (int i = 0; i < jsonArray.size(); i++) {
                    strings[i] = jsonArray.getString(i);
                }
                return strings;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String[] getStringArr(String json, String key) {
        try {
            JSONObject jsonObject = JSON.parseObject(json);
            JSONArray jsonArray = jsonObject.getJSONArray(key);
            if (jsonArray != null) {
                String[] strings = new String[jsonArray.size()];
                for (int i = 0; i < jsonArray.size(); i++) {
                    strings[i] = jsonArray.getString(i);
                }
                return strings;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
